<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<div id="app">
    <template>


        <el-row style="margin-top: 1%">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="readAll"
                >
                    一键已读
                </el-button>
            </el-col>
        </el-row>


        <el-table
                v-loading="loading"
                :data="tableData" row-key="id"
                :header-cell-style="{'text-align':'center','color':'#303133','font-weight':'600','background-color': '#F5F7FA'}"
                :cell-style="{'text-align':'center'}" border style="margin-top: 10px"
        >
            <el-table-column label="编号" align="center" prop="id"></el-table-column>
            <el-table-column label="发送方" align="center" prop="senderName"></el-table-column>
            <el-table-column label="标题" align="center" prop="title"></el-table-column>
            <el-table-column label="内容" align="center" prop="content"></el-table-column>
            <el-table-column label="消息状态" align="center" prop="status">
                <template slot-scope="scope">
                    <span v-if="scope.row.status===1" style="color: green">已读</span>
                    <span v-else style="color: red"> 未读</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ scope.row.createTime}}</span>
                </template>
            </el-table-column>

        </el-table>
        <el-pagination
                background
                :page-sizes="[10, 20, 30, 40,50]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                :page-size.sync="pageSize"
                :current-page.sync="currentPage"
                @size-change="handleQuery"
                @current-change="handleQuery"
                style="float: right;margin-top: 10px"
        />
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/admin/sysMessage.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],

                // 消息状态字典
                statusOptions: [],
                button: {
                    add: false,
                    update: false,
                    delete: false
                },
                loading: false,
                queryForm: {
                    roleName: ''
                }
            }
        },
        created() {
            this.handleQuery();
            let sysButtons = JSON.parse(localStorage.getItem("mars-sysButtons"));
            this.button.add = sysButtons.indexOf("admin/sysMessage/add") > -1;
            this.button.update = sysButtons.indexOf("admin/sysMessage/update") > -1;
            this.button.delete = sysButtons.indexOf("admin/sysMessage/delete") > -1;
            getDicts("msg_read_type").then(response => {
                this.statusOptions = response.data;
            });

        },
        methods: {
            switchChange(row) {
                this.formData = row
                update(this.formData).then(res => {
                    if (res.code === '200') {
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            type: 'success'
                        });
                    }
                })
            },

            statusFormat(row, column) {
                return selectDictLabel(this.statusOptions, row.status);
            },
            resetQuery() {
                this.queryForm = {}
                this.handleQuery()
            },

            readAll() {
                requests.get("/admin/sysMessage/readAllMsg").then(res => {
                    if (res.code === '200') {
                        this.$notify({
                            title: '成功',
                            message: '已读成功',
                            type: 'success'
                        });
                    }
                    setTimeout(() => {
                        let index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }, 1000)
                })
            },

            handleCurrentChange(val) {
                this.currentPage = val;
                this.handleQuery();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleQuery();
            },
            handleQuery() {
                let queryParam = this.queryForm
                queryParam.pageNo = this.currentPage
                queryParam.pageSize = this.pageSize
                this.loading = true
                pageList(queryParam).then((res) => {
                    if (res.code === '200') {
                        this.tableData = res.data.list;
                        this.total = res.data.count;
                        this.loading = false
                    } else {
                        this.loading = false;
                        this.$notify({
                            title: '失败',
                            message: res.message,
                            type: 'error'
                        });
                    }
                });
            },
            handleDelete(row) {
                deleteById('/admin/sysMessage/delete/' + row.id, this);
            },

            handleAddOrUpdate(type, row) {
                if (type === 1) {
                    openPage('新增消息', '/admin/sysMessage/sysMessageAddUpdate.html', this);
                } else {
                    let params = row
                    params.type = 1
                    requests.post("/admin/sysMessage/update", params).then(res => {
                        if (res.code === '200') {
                            this.$notify({
                                title: '成功',
                                message: '操作成功',
                                type: 'success'
                            });
                            this.handleQuery();
                        }
                    })
                }
            },
        }
    });
</script>
</html>
